package com.city.controller.business;

import com.city.config.BaiduMap;
import com.city.config.Status;
import com.city.domain.pub.PubUser;
import com.city.domain.report.CtReport;
import com.city.domain.report.CtTask;
import com.city.format.CtReportFormat;
import com.city.format.CtReportFormatSql;
import com.city.helper.AuthHelper;
import com.city.helper.TimeStampHelper;
import com.city.repository.report.CtReportFormatSqlRepository;
import com.city.repository.report.CtUserBranchRepository;
import com.city.repository.report.format.CtUserFoematRepository;
import com.city.service.pub.PubUserService;
import com.city.service.report.CtTaskService;
import com.city.service.report.ReportService;
import com.city.helper.FormatHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @deprecation 业务处置
 * @email leichuqiao@126.com
 * @author 雷楚桥
 * @date 2018-07-06 19:49
 */
@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private FormatHelper formatHelper;
    @Autowired
    private AuthHelper authHelper;
    @Autowired
    private CtUserBranchRepository ctUserBranchRepository;
    @Autowired
    private PubUserService pubUserService;
    @Autowired
    private CtReportFormatSqlRepository sqlRepository;
    @Autowired
    private CtTaskService ctTaskService;
    @Autowired
    private CtUserFoematRepository ctUserFoematRepository;




    /**
     * @deprecation 主页
     * @author 雷楚桥
     * @date 2018-07-06 19:50
     */
    @RequestMapping("/index")
    public String index(Map<String,Object> map){
        return "/business/index";
    }

    /**
     * @deprecation 详情浏览
     * @author 雷楚桥
     * @date 2018-07-06 21:47
     */
    @RequestMapping("/see")
    public String detail(Map<String,Object> map, @Param("page") Integer page, HttpSession session){

        //网格员上报
        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(1, 7,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;



        List<CtReportFormatSql> ctReportFormatSqls = sqlRepository.findDiaryByBranch(1,7,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, ctReportFormatSqls);

        map.put("currentPage", page);
        map.put("reportList", ctReportFormatSqls);
        map.put("titleName","业务处置详情浏览");
        return "/business/see";
    }

    /**
     * @deprecation 业务处置详情信息
     * @param id Integer 上报id
     * @return Map
     * @author 雷楚桥
     * @date 2018/7/29 18:22
     */
    @RequestMapping("/detail")
    public String detail(@Param("id") Integer id,
                         Map<String,Object> map){

        //详情
        CtReport ctReport = reportService.findReportById(id);
        CtReportFormat reportFormat = formatHelper.ctReportFormat(ctReport);
        if(ctReport.getCheckManId() != null){
            reportFormat.setCheckManName(ctUserFoematRepository.findByUserId(ctReport.getCheckManId()).getName());
        }
        String taskId = ctReport.getTaskId();



        map.put("taskId", taskId);
        map.put("endTime",ctReport.getDeadline());

        //获取时间
        CtTask ctTask = ctTaskService.findByTaskId(id);

        if(ctReport.getIsrework() == 1){
            map.put("isrework",ctTask.getReworkComment());
        }else {
            map.put("isrework",null);
        }

        map.put("taskTime", ctTask);

        map.put("reportFormat",reportFormat);

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "业务处置详情信息");
        return "/business/detail";
    }

    /**
     * @deprecation 指挥派遣
     * @author 雷楚桥
     * @date 2018/7/29 18:32
     */
    @RequestMapping("/dispatch")
    public String dispatch(Map<String,Object> map,@Param("page") Integer page,HttpSession session){

        //网格员上报 TODO 没有根据部门进行检索
        /**
         * @author  王宇
         * @date 2018/9/9
         * @description  补充部门信息
         */

        //网格员上报
        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(1, 4,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;

        List<CtReportFormatSql> ctReportFormatSqls = sqlRepository.findDiaryByBranch(1,4,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, ctReportFormatSqls);
        map.put("currentPage", page);

        map.put("titleName","指挥派遣");
        return "/business/dispatch";
    }

    /**
     * @deprecation 处置反馈
     * @author 雷楚桥
     * @date 2018-08-12 11:31
     */
    @RequestMapping("/back")
    public String back(Map<String,Object> map,@Param("page") Integer page,HttpSession session){
        //查询所有能够反馈的处置
        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(2, 5,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;


        List<CtReportFormatSql> ctReportFormatSqls = sqlRepository.findDiaryByBranch(2,5,authHelper.thisBranchId(), startRow, pageSize);
//        ctReportFormatSqls.stream().forEach(ctReportFormatSql -> ctReportFormatSql.setCreateTimeName(new TimeStampHelper().stampToDate(ctReportFormatSql.getCreateTimeName())));

        MobileController.updateSqs(map, ctReportFormatSqls);

        map.put("currentPage", page);
        map.put("titleName","处置反馈");
        return "/business/back";
    }
    
    /**
     * @deprecation 处置督办
     * @author 雷楚桥
     * @date 2018-08-12 16:30
     */
    @RequestMapping("/do_ban")
    public String do_ban(Map<String,Object> map,@Param("page") Integer page,HttpSession session){

        //每页显示的条数
        long pageSize = 15;


        long total = sqlRepository.countReportByLeft(1, 5,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;


        //网格员上报

        List<CtReportFormatSql> sqs = sqlRepository.findDiaryByBranch(1,5,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, sqs);

        map.put("currentPage", page);
        map.put("titleName", "处置督办");
        return "/business/do_ban";
    }
    
    /**
     * @deprecation 流程查看
     * @author 雷楚桥
     * @date 2018-08-22 16:31
     */
    @RequestMapping("/process")
    public String process(Map<String,Object> map){
        map.put("titleName", "流程查看");
        return "/business/process";
    }

    /**
     * @deprecation 地图操作
     * @author 雷楚桥
     * @date 2018-09-03 17:03
     */
    @RequestMapping("/map")
    public String map(Map<String,Object> map){
        map.put("AK",BaiduMap.BAIDU_AK);
        map.put("titleName", "地图操作");
        return "/business/map";
    }

    /**
     * @deprecation 软件支持
     * @author 雷楚桥
     * @date 2018-09-03 17:35
     */
    @RequestMapping("/software")
    public String software(Map<String,Object> map){
        map.put("titleName", "软件支持");
        return "/business/software";
    }
    
    /**
     * @deprecation 业务信息打印
     * @author 雷楚桥
     * @date 2018-09-04 9:06
     */
    @RequestMapping("/print")
    public String print(Map<String,Object> map,@Param("page") Integer page,HttpSession session){
        //网格员上报
        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(1, 7,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;


        //网格员上报
        List<CtReportFormatSql> sqs = sqlRepository.findDiaryByBranch(1,7,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, sqs);

        map.put("currentPage", page);
        map.put("titleName", "业务信息打印");
        return "/business/print";
    }

    /**
     * @deprecation 打印详情
     * @param id Integer 上报id
     * @author 雷楚桥
     * @date 2018-09-04 9:36
     */
    @RequestMapping("/print_detail")
    public String print_detail(@Param("id") Integer id,
                                Map<String,Object> map){

        //详情
        CtReport ctReport = reportService.findReportById(id);
        map.put("reportFormat",formatHelper.ctReportFormat(ctReport));

        map.put("AK", BaiduMap.BAIDU_AK);
        map.put("titleName", "打印");
        return "/business/print_detail";
    }

    /**
     * @deprecation 申请会办
     * @author 雷楚桥
     * @date 2018-09-04 10:42
     */
    @RequestMapping("/together")
    public String together(Map<String,Object> map,@Param("page") Integer page,HttpSession session){

        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(3, 5,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;


        //网格员上报
        List<CtReportFormatSql> sqs = sqlRepository.findDiaryByBranch(1,4,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, sqs);

        map.put("currentPage", page);



        map.put("titleName", "申请会办");
        return "/business/together";
    }

    /**
     * @deprecation 申请授权
     * @author 雷楚桥
     * @date 2018-09-04 10:42
     */
    @RequestMapping("/auth")
    public String auth(@Param("page") Integer page,
                       HttpSession session,
                       Map<String,Object> map){

        //每页显示的条数
        long pageSize = 15;

        long total = sqlRepository.countReportByLeft(1, 4,authHelper.thisBranchId());
        //查到记录总数
        map.put("reportNum", total);

        //总页数
        long pageTimes;
        if (total % pageSize == 0) {
            pageTimes = total / pageSize;
        } else {
            pageTimes = total / pageSize + 1;
        }
        map.put("pageTimes", pageTimes);
        session.setAttribute("pageTimes", pageTimes);

        //页面初始的时候page没有值
        if (null == page) {
            page = 1;
        }

        //每页开始的第几条记录
        long startRow = (page - 1) * pageSize;

        //网格员上报
        List<CtReportFormatSql> sqs = sqlRepository.findDiaryByBranch(1,4,authHelper.thisBranchId(), startRow, pageSize);
        MobileController.updateSqs(map, sqs);
        map.put("currentPage", page);
        map.put("titleName", "申请授权");
        return "/business/auth";
    }

}
